class Solution {
    int sum;
    int path;
    public int subsetXORSum(int[] nums) {
        sum = 0;
        path = 0;
        dfs(nums, 0);
        return sum;
    }

    /**
     * 1863. 找出所有子集的异或总和再求和
     * https://leetcode.cn/problems/sum-of-all-subset-xor-totals/description/
     * @param nums
     * @param pos
     */
    void dfs(int[] nums, int pos) {
        sum += path;
        for(int i = pos; i < nums.length; i++) {
            path ^= nums[i];
            dfs(nums, i + 1);
            path ^= nums[i];//恢复现场
        }
    }
    //第二种方法
    void dfs2(int[] nums, int pos) {
        if(pos == nums.length) {
            return;
        }
        //选
        path ^= nums[pos];
        sum += path;
        dfs(nums, pos + 1);
        //不选
        path ^= nums[pos];//恢复现场（回溯）
        dfs(nums, pos + 1);
    }
}